//# include <iostream>
//# include <vector>
//# include <map>
////# include<bits/stdc++.h>
//using namespace std;
////Memory Limit Exceeded
////int getRes(int n, int a, int b)
////{
////    if (n == 1 || n == 2)
////    {
//////        return n;
////        return 1;
////    }
////    return (a * getRes(n - 1, a, b) + b * getRes(n - 2, a, b)) % 7;
////}
//int main()
//{
//    int n, a, b;
//    while (cin >> a >> b >> n)
//    {
//        if (!n && !a && !b)
//        {
//            break;
//        }
//        //Memory Limit Exceeded
////        vector<int> rec(n);
////        rec[0] = rec[1] = 1;
////        for (int i = 2; i < n; ++i)
////        {
////            rec[i] = (a * rec[i - 1] + b * rec[i - 2]) % 7;
////        }
////        cout << rec[n - 1] << endl;
//        // TLE
//        int l = 1, r = 1;
////        for (int i = 2; i < n % 6; ++i)
//        for (int i = 2; i < n % 48; ++i) // 48是周期？好像数据有问题
//        {
//            int tmp = (a * r + b * l) % 7;
//            l = r;
//            r = tmp;
//        }
//        cout << r << endl;
//    }
//    return 0;
//}

// 用这个程序可以算周期
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
    int A, B, n, i;
    int f[55] = {0, 1, 1};  //f[1]、f[2]均为 1
    while(scanf("%d%d%d", &A, &B, &n), A) {
        A %= 7;
        B %= 7;
        f[3] = (A + B) % 7;
        f[4] = (A * f[3] + B) % 7;
        for(i = 5; i <= n; i++) { //判断条件是i <= n是因为当n < T + 4 (T为周期)时还没算出周期，而此时已经把f(n)算出来的，就没有必要再算周期了
            f[i] = (A * f[i - 1] + B * f[i - 2]) % 7;
            if(f[i] == f[4] && f[i - 1] == f[3]) {
                n = (n - 3) % (i - 4) + 3; //注意 i - 4 才是周期，加3是因为f(3)才是周期的第一个数
                break;
            }
            cout << i << f[i] << endl;
        }
        printf("%d\n", f[n]);
    }
    return 0;
}
